<template>
  <div>
    <transition name="el-fade-in">
      <div class="proper__model" v-if="show" @click="hideProper"></div>
    </transition>
    <transition @before-enter="beforeEnter" @enter="enter" @leave="leave">
      <div
        :class="['proper__content', `proper__${placement}`]"
        :style="getClient()"
        v-if="show"
      >
        <div class="title">
          <template v-if="title">
            <span class="icontitle iconfont" :class="icon" /> {{ title }}
          </template>
          <span v-else></span>
          <span
            title="关闭"
            class="close iconfont icon-chacha"
            @click="hideProper"
          ></span>
        </div>
        <div class="title-height"></div>
        <div class="content-view">
          <slot></slot>
        </div>
      </div>
    </transition>
  </div>
</template>

<script>
/**
 * 边界滑出层
 * @desc 在页面中弹出一个遮罩层，与一快内容区域
 * @param 见props
 * @example <slide-model v-model="showProper" placement="right" client="300">这里是content</slide-model>
 */
export default {
  name: "search-model",
  data() {
    return {
      show: this.value,
    };
  },
  watch: {
    value(val) {
      console.log(val);
      this.show = val;
    },
  },
  props: {
    value: {
      // 是否显示
      type: Boolean,
      default: false,
    },
    title: {
      type: String,
      default: "",
    },
    icon: {
      type: String,
      default: "icon-bianji",
    },
    placement: {
      type: String, // content出现位置
      default: "right", // {left,top,bottom,right}
    },
    client: {
      // 内容区域的高度或者宽度，左边或者右边表示宽度，上边或者下边表示高度
      type: [String, Number],
      default: 300,
    },
  },
  methods: {
    /**
     * 获取定位确定固定高或宽样式
     * @return {Object} style 内容区域高或宽样式
     */
    getClient() {
      let style = {};
      if (this.placement === "top" || this.placement === "bottom") {
        style = {
          height: this.client + "px",
        };
      } else {
        style = {
          width: this.client + "%",
        };
      }
      return style;
    },
    /**
     * 隐藏弹窗
     */
    hideProper() {
      this.show = false;
      this.$emit("input", this.show);
    },
    beforeEnter(el) {
      switch (this.placement) {
        case "left":
          el.style.transform = `translateX(-${this.client}px)`;
          break;
        case "right":
          el.style.transform = `translateX(${this.client}px)`;
          break;
        case "top":
          el.style.transform = `translateY(-${this.client}px)`;
          break;
        case "bottom":
          el.style.transform = `translateY(${this.client}px)`;
          break;
      }
      el.style.opacity = 0;
    },
    enter(el, done) {
      done();
      el.style.transition = "all .3s ease";
      el.style.transform = "translateX(0px) translateY(0px)";
      el.style.opacity = 1;
    },
    leave(el, done) {
      el.style.transition = "all .2s ease";
      switch (this.placement) {
        case "left":
          el.style.transform = `translateX(-${this.client}px)`;
          break;
        case "right":
          el.style.transform = `translateX(${this.client}px)`;
          break;
        case "top":
          el.style.transform = `translateY(-${this.client}px)`;
          break;
        case "bottom":
          el.style.transform = `translateY(${this.client}px)`;
          break;
      }
      el.style.opacity = 0;
      setTimeout((_) => {
        done();
      }, 1500);
    },
  },
};
</script>

<style lang="scss" scoped>
.title {
  height: 40px;
  line-height: 40px;
  color: #384681;
  font-size: 16px;
  border-bottom: 1px solid #e4ebf1;
  position: absolute;
  font-weight: bold;
  padding: 0 10px;
  width: 98%;
  left: 50%;
  margin-left: -49%;
  .icontitle {
    font-size: 16px;
    color: #384681;
    font-weight: normal;
  }
  .close {
    position: absolute;
    right: 0px;
    height: 20px;
    line-height: 20px;
    top: 50%;
    margin-top: -10px;
    width: 20px;
    text-align: center;
    font-size: 20px;
    border-radius: 20px;
    cursor: pointer;
    color: #384681;
  }
  .close:hover {
    color: #647bdc;
  }
}
.content-view {
  height: 100%;
  overflow-y: scroll;
  padding: 0 10px 100px 10px;
}
.content-view::-webkit-scrollbar {
  width: 4px;
}
.content-view::-webkit-scrollbar-thumb {
  border-radius: 10px;
  -webkit-box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.2);
  background: rgba(0, 0, 0, 0.2);
}
.content-view::-webkit-scrollbar-track {
  -webkit-box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.2);
  border-radius: 0;
  background: rgba(0, 0, 0, 0.1);
}
.content-view::-webkit-scrollbar {
  display: none;
}
.title-height {
  height: 50px;
}
.footer-height {
  height: 70px;
}
.proper__model {
  position: fixed;
  height: 100%;
  width: 100%;
  top: 0;
  left: 0;
  z-index: 1999;
  background-color: rgba(0, 0, 0, 0.3);
}
.proper__content {
  position: fixed;
  overflow: hidden;
  z-index: 2000;
  padding: 10px 0;
  box-shadow: 0 0 3px #fff;
  background-color: #fff;
}
.proper__left {
  bottom: 0;
  left: 0;
  height: 100%;
  padding-top: 20px;
}
.proper__right {
  bottom: 0;
  right: 0;
  height: 100%;
  padding-top: 0;
}
.proper__top {
  top: 0;
  left: 0;
  width: 100%;
  padding-top: 20px;
}
.proper__bottom {
  bottom: 0;
  left: 0;
  width: 100%;
  padding-top: 20px;
}
</style>